package net.sk.china.city.service.impl;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import net.sk.china.city.domain.dto.activity.ActivityInfoDTO;
import net.sk.china.city.domain.dto.activity.ActivitySearchDTO;
import net.sk.china.city.domain.dto.activity.ActivityUpdateDTO;
import net.sk.china.city.domain.dto.activity.Job;
import net.sk.china.city.mapper.ArActivityRecordMapper;
import net.sk.china.city.utils.ConvertUtil;
import com.github.pagehelper.PageInfo;
import net.sk.china.city.domain.dto.TableDataInfo;
import net.sk.china.city.utils.PlatformIdHolder;
import net.sk.china.city.service.IArActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import net.sk.china.city.mapper.ArActivityMapper;
import net.sk.china.city.domain.ArActivity;
import org.springframework.transaction.annotation.Transactional;

/**
 * 活动Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-07
 */
@Service
public class ArActivityServiceImpl implements IArActivityService
{
    @Autowired
    private ArActivityMapper arActivityMapper;
    @Autowired
    private ArActivityRecordMapper arActivityRecordMapper;

    /**
     * 查询活动
     *
     * @param pkId 活动主键
     * @return 活动
     */
    @Override
    public ActivityInfoDTO selectArActivityByPkId(Long pkId)
    {
        ArActivity arActivity = arActivityMapper.selectArActivityByPkId(pkId);
        return ActivityInfoDTO.from(arActivity);
    }

    /**
     * 查询活动列表
     *
     * @param activitySearchDTO 活动
     * @return 活动
     */
    @Override
    public TableDataInfo<ActivityInfoDTO> selectArActivityList(ActivitySearchDTO activitySearchDTO)
    {
        List<ArActivity> activityList = arActivityMapper.selectArActivityList(activitySearchDTO);
        PageInfo<ArActivity> arActivityPageInfo = new PageInfo<>(activityList);
        List<ActivityInfoDTO> activityInfoDTOList = ConvertUtil.converList(activityList, ActivityInfoDTO::from);
        return new TableDataInfo<>(activityInfoDTOList, arActivityPageInfo.getTotal());
    }

    /**
     * 新增活动
     * 
     * @param activityUpdateDTO 活动
     * @return 结果
     */
    @Transactional
    @Override
    public int insertArActivity(ActivityUpdateDTO activityUpdateDTO)
    {
        String platformId = PlatformIdHolder.getPlatformId();
        String[] platformIdArray = platformId.split(",");
        int platformIdArrayLength = platformIdArray.length;
        // todo 需要再将安源红降一级，或者修改若依代码把0作为系统超级管理员级别
        // 比如总站是第二级：0(无),100(安源红),200(总站),201(镇/街),202(社区/村)，
        if (platformIdArrayLength <= 3) {
            // 总站，直接发布活动
            activityUpdateDTO.setPkId(IdUtil.getSnowflakeNextId());
            activityUpdateDTO.setStatus(2L);
            activityUpdateDTO.setPlatformId(platformId);
            ArActivity activity = activityUpdateDTO.toActivity();
            arActivityMapper.insertArActivity(activity);
            List<Job> jobList = activityUpdateDTO.getJob();
            for (Job job : jobList) {
                long snowflakeNextId = IdUtil.getSnowflakeNextId();
                job.setId(snowflakeNextId);
            }
            return arActivityRecordMapper.insertJobList(jobList, activity.getPkId());
        }
        if (platformIdArrayLength <= 4) {
            // 镇街，申请活动
            activityUpdateDTO.setPkId(IdUtil.getSnowflakeNextId());
            activityUpdateDTO.setStatus(1L);
            activityUpdateDTO.setPlatformId(platformId);
            ArActivity activity = activityUpdateDTO.toActivity();
            arActivityMapper.insertArActivity(activity);
            List<Job> jobList = activityUpdateDTO.getJob();
            for (Job job : jobList) {
                long snowflakeNextId = IdUtil.getSnowflakeNextId();
                job.setId(snowflakeNextId);
            }
            return arActivityRecordMapper.insertJobList(jobList, activity.getPkId());
        }

        return 1;
    }

    /**
     * 修改活动
     * 
     * @param activityUpdateDTO 活动
     * @return 结果
     */
    @Transactional
    @Override
    public int updateArActivity(ActivityUpdateDTO activityUpdateDTO)
    {
        ArActivity activity = activityUpdateDTO.toActivity();
        int i = arActivityMapper.updateArActivity(activity);
        List<Job> jobList = activityUpdateDTO.getJob();
        for (Job job : jobList) {
            // 更新对应的岗位
            int updatedCount = arActivityRecordMapper.updateJob(job, activity.getPkId());
            // 更新失败，那么就插入岗位
            if (updatedCount == 0) {
                long snowflakeNextId = IdUtil.getSnowflakeNextId();
                job.setId(snowflakeNextId);
                arActivityRecordMapper.insertJob(job, activity.getPkId());
            }
        }
        return i;
    }

    /**
     * 批量删除活动
     * 
     * @param pkIds 需要删除的活动主键
     * @return 结果
     */
    @Override
    public int deleteArActivityByPkIds(Long[] pkIds)
    {
        return arActivityMapper.deleteArActivityByPkIds(pkIds);
    }

    /**
     * 删除活动信息
     * 
     * @param pkId 活动主键
     * @return 结果
     */
    @Override
    public int deleteArActivityByPkId(Long pkId)
    {
        return arActivityMapper.deleteArActivityByPkId(pkId);
    }

    @Override
    public int deleteJobById(Long jobId) {
        return arActivityMapper.deleteJobByJobId(jobId);
    }

    @Override
    public int passActivity(Long activityId) {
        return arActivityMapper.passActivity(activityId);
    }

    @Override
    public int denyActivity(Long activityId) {
        return arActivityMapper.denyActivity(activityId);
    }

    @Override
    public TableDataInfo<ActivityInfoDTO> selectActivityApproveList(ActivitySearchDTO activitySearchDTO) {
        String platformId = PlatformIdHolder.getPlatformId();
        String[] platformIdArray = platformId.split(",");
        int platformIdArrayLength = platformIdArray.length;
        // 比如总站是第二级：0(无),100(安源红),200(总站),201(镇/街),202(社区/村)

        List<ArActivity> activityList = arActivityMapper.selectArActivityList(activitySearchDTO);
        PageInfo<ArActivity> arActivityPageInfo = new PageInfo<>(activityList);
        List<ActivityInfoDTO> activityInfoDTOList = ConvertUtil.converList(activityList, ActivityInfoDTO::from);
        return new TableDataInfo<>(activityInfoDTOList, arActivityPageInfo.getTotal());
    }
}
